
// //请求截图
// if(!requestScreenCapture(true)){
//     toast("请求截图失败");
//     exit();
// }else{
//     log('截图权限')
// }
// $images.requestScreenCapture();

// var imgs = {}
// imgs.find = function(name){

//     try {
//         //截图
//         // 初始化ColorMapping
//         let ColorMapping = $colors.mapping;
//         // 创建ColorMapping实例
//         let cm = new ColorMapping();
//         // 截屏
//         let img = $images.captureScreen();
//         // 初始化颜色映射
//         cm.reset(img);
//         let cd = d[name]

//         let newoption = cd[2]



//         width = newoption["region"][2] - newoption["region"][0]
//         height = newoption["region"][3] - newoption["region"][1]

//         log(cd[2], newoption, width, height)

//         var p = cm.findMultiColors(cd[0],cd[1], { region:[newoption["region"][0],newoption["region"][1],width,height],threshold: 20 } )
//         cm.recycle();

//         if ( p ){///////////////
//             log('找到=> '+ name)
//             if ( cd[3] ){
//                 if (!cd[4]) { cd[4] = 1 }
//                 f.click(p.x,p.y,cd[4],name )
//             }
//             return p
//         }
//     } catch (error) {
//         console.log(error)
//     }

// }


//新版基础函数
var f = {
    init:function() {
        log("程序初始化")
    },
    /**
     * 读取本地数据。
     * @param {string} name - 存储对象的名称。
     * @param {string} key - 存储对象的键。
     * @returns {*} - 返回存储的数据，如果不存在则返回undefined。
     */
    getLocal: function(key) {
        const storage = storages.create('ABC');
        if (storage.contains(key)) {
            return storage.get(key);
        }
    },
    /**
     * 读取本地数据。
     * @param {string} name - 存储对象的名称。
     * @param {string} key - 存储对象的键。
     * @returns {*} - 返回存储的数据，如果不存在则返回undefined。
     */
    setLocal:function (key, value) {
        const storage = storages.create('ABC');
        return storage.put(key, value);
    },
    //post
    post : function(url,data){
        var res = http.post(url, data);
        var data = res.body.string();
        if(data){
            // log(data);
            return data;
        }
    },
    //get
    get : function(url){
        var res = http.get(url);
        var data = res.body.string();
        if(data){
            return data;
        }
    },
    //读取本地数据
    getLocal:function ( name, key) {
        const storage = storages.create(name);  //创建storage对象
        if (storage.contains(key)) {
            return storage.get(key);
        };
        //默认返回undefined
    },
    //基础函数
    active:function(pkg,n){
        if(!n){n=5}
        if(  currentPackage() == pkg ){
            log("应用在前端");
            return true;
        }else{
            log("启动一次应用");
            app.launch(pkg);
            sleep(1000)
            f.ms({"textMatches":"允许|始终允许"},true)
            sleep(1000*n)
        }
    },
    //准备点击
    click:function (x,y,sleeptime,name){
        if ( ! sleeptime ){sleeptime = 1}
        if ( name ){
            log('准备点击->'+name,"x:",x,"y:",y);
        }else{
            log('准备点击坐标->', "x:",x,"y:",y);
        }
        if( x > 0  && y > 0  ){
            threads.start(function(){
                //在新线程执行的代码
                click(x,y);
            });
            // click(x,y);
            sleep(sleeptime*1000);
            return true
        }else{
            log('坐标错误');
        }
    },
    //点击obj
    clickObj:function (obj,sleeptime,name){
        if ( ! sleeptime ){ sleeptime == 1 } 
        log(  name ? ("准备点击对象->" + name) : "点击未命名对象" )
        x = obj.bounds().centerX()
        y = obj.bounds().centerY()
        return f.click(x,y,sleeptime,name)
    },
    //穿透点击
    clickTrue:function(obj,sleeptime,name,lun){
        sleeptime ? sleeptime : 1
        let result = false;
        lun ? lun : 3
        for (let i=0;i<lun;i++){
            if (  obj && obj.clickable() ){
                obj.click();
                log(  name ? ("准备穿透点击对象->" + name) : "准备穿透点击未命名对象" )
                result = true
                break
            }
            obj = obj.parent()
        }
        if ( result ) { sleep(sleeptime * 1000) }
        return result
    },
    //正则点击
    ms:function (obj,clicks,sleeptimes,name,findtime,lun){
        if (!sleeptimes) { sleeptimes = 1}
        if (!findtime) { findtime = 200}
        if (!lun) { lun = 3}

        var txt = '';
        for(let key in obj){
            if ( key.search("Matches") > 0 ){
                eval("var matches = /" + obj[key] + "/")
                txt = txt + key+'('+matches+').'
            }else if( key ==  'boundsInside' ){
                txt = txt + key+'('+obj[key]+').'
            }else{
                txt = txt + key+'("'+obj[key]+'").'
            }
        }
        var txt = "let objs = "+txt+"findOne("+findtime+");"
        log(txt)
        eval(txt)
        if ( objs ) {
            log( '找到==>' + objs.text() || objs.desc() || objs.id() || objs.className() )
            if ( clicks ){
                name = obj['textMatches'] || obj['descMatches'] || obj['idMatches'] || obj['text'] || obj['desc'] || obj['id']
                if (lun < 1 || !f.clickTrue( objs,sleeptimes,name,lun )){
                    log('准备坐标点击')
                    return f.clickObj( objs,sleeptimes,name );
                }
            }
            return true;
        }
    },
    rd:function (min,max){
        if (min<=max){
            return random(min,max)
        }else{
            return random(max,min)
        }
    },
    moveTo:function (x,y,x1,y1,times){
        swipe(x,y,x1,y1,times);
        sleep(1000);
    },
    getTextById: function (idstr) {
        try {
            return id(idstr).findOne(200).text()
        } catch (e) {
            log(e)
        }
    }
}
f.init()


//批查打印app参数
function printAll( ways ) {
    if (!ways){
        ways = 'className'
    }
    if (ways = 'className'){
        var all_Info = classNameMatches(/.*/).find();
    }else
    if (ways == 'text'){
        var all_Info = textMatches(/.*/).find();
    }else
    if (ways == 'id'){
        var all_Info = idMatches(/.*/).find();
    }else{
        eval( 'var all_Info = ' + ways + '.find();'  )
    }
    
    if ( all_Info ){
        for (var i = 0;i<all_Info.length;i++){
            var d = all_Info[i];
            log(i,d.id(),d.text(),"desc:"+d.desc(),'"className":"'+d.className()+'"',
            "clickable->"+d.clickable(),'selected->'+selected(),"depth->"+d.depth(),
            d.bounds().centerX(),d.bounds().centerY())
        }
    }else{
        log('没有找到任何相关的节点')
    }
}

printAll()



function sbzs() {
    var phones = [
    //   'samsung',
    //   'Xiaomi',
    //   'Sony',
      'OPPO',
    //   'XIAOMI',
    //   'vivo',
    //   'HUAWEI',
    //   'HONOR'
    ]
  
    var i = 0
    let key = false
    while (i < 30) {
      f.active(appinfo.sbzs, 5)
      f.ms({ "text": "一键新机" }, true, 2)
  
      if (f.ms({ "text": "当前设备" }, false, 2)) {
  
        if (key) {
          sleep(1000)
          return true
        }
  
        var ids = [
          'phone_vendor',
          'phone_brand',
          'phone_model',
          'phone_serial_number',
          'android_id',
          'imsi',
          'wifi_mac',
          'op_name',
          'btaddr',
          'displayid',
          'baseband',
        ]
  
        appinfo.device = {}
        for (let Ii = 0; Ii < ids.length; Ii++) {
          appinfo.device[ids[Ii]] = f.getTextById(ids[Ii])
        }
  
        f.ms({ "text": "修改设备信息" }, true, 2)
      }
      else if (f.ms({ "text": "设备属性设置" }, false, 2)) {
        f.ms({ "text": "指定机型" }, true, 2)
        f.ms({ "id": "brand" }, true, 2)
  
        f.ms({ "text": "OPPO", 'id': 'brand' }, true)
        f.ms({ "text": "下一步" }, true, 2)
  
      } else if (f.ms({ "text": "立即清理" }, false, 2)) {
        key = true
        f.ms({ "text": appinfo.appname }, true, 2)
        f.ms({ "text": "立即清理" }, true, 2)
      }
  
      f.ms({ "text": "OPPO", 'id': 'brand' }, true)
      sleep(1000)

      i++
    }
}


function back_info(){
    let appids = "com.keramidas.TitaniumBackup"
    let appnames = "钛备份"

    var i = 0
    let key = false
    while (i < 40) {
        log("准备备份")
        f.active(appids, 5)
        f.ms({ "textMatches": "不要再显示此内容.*" }, true, 2)
        f.ms({ "textMatches": "确定" }, true, 2)
        if (f.ms({ "text": "正在备份" })) {

        }else{
            if ( f.ms({ "text": "备份/还原" }, false, 2) ){
                if ( key ){
                    return true
                }

                f.ms({ "text": "备份/还原" }, true, 2)
                f.ms({ "textMatches": "锦江荟.*" }, true, 2)
                if ( f.ms({ "text": "备份！" }, true, 2)){
                    key = true
                }
            }
        }
        sleep(1000)
        i++
    }
}

// function notice(txt){
//     var url = 'https://oapi.dingtalk.com/robot/send?access_token=c230bf201cf694db851c14413af45f3c216754466886f7f46647eb3678d809a4'
//     var postData = {"msgtype": "text","text": {"content":`通知，${txt}`}}
//     http.postJson(url, postData)
// }

function bdarr(arr, txt){
    for (let index = 0; index < arr.length; index++) {
        if (arr[index] == txt ){
            return true
        }
    }
}

function active_info(){
    let appids = "com.keramidas.TitaniumBackup"
    let appnames = "钛备份"

    var i = 0
    let key = false
    while (i < 40) {
        log("准备备份")
        f.active(appids, 5)
        f.ms({ "textMatches": "不要再显示此内容.*" }, true, 2)
        f.ms({ "textMatches": "确定" }, true, 2)
        if (f.ms({ "textMatches": "正在恢复.*" })) {

        }else{
            if ( f.ms({ "text": "备份/还原" }, false, 2) ){
                if ( key ){
                    return true
                }else{
                    f.ms({ "text": "备份/还原" }, true, 2)
                }

                f.ms({ "textMatches": "锦江荟.*" }, true, 2)

            }else{
                if( f.ms({ "textMatches": "备份属性" }) ){
              
                    var list = textMatches("2024.*").find()
                    if( list ){
                        log("准备点击")

                        var look = true

                        for (var ir = 0; ir < list.length; ir++) {
                            console.log(ir, list[ir].text())
                            // console.log( "read_list", read_list, typeof(read_list) )

                            if ( !bdarr( read_list, list[ir].text()) ){

                                var ds = list[ir]
                                var obj = ds.parent().parent().children()[0]
                                
                                f.clickObj(obj, 2, list[ir].text() )
                                read_list.push( list[ir].text() )
                         
                                look = false
                                break
                            }else{
                                log("已经激活过")
                            }
                        }

                        if ( look ){
                            f.moveTo(width*0.5, height * 0.8, width*0.5, height * 0.3, 2000)
                        }
    
                        if(  f.ms({ "textMatches": "程序.*数据" }, true, 2) ){
                        // if(  f.ms({ "textMatches": "程序.*数据" }, false, 2) ){
                            // back()
                            key = true
                        }
                    }
                }
            }
        }
        sleep(1000)
        i++
    }
}


//上传idfa
function Idfa(other){
    let postArr = {};
    postArr['service'] = 'Idfa.idfa';
    postArr['name'] = appinfo.name;
    postArr['idfa'] = appinfo.device.android_id;
    postArr['account'] = appinfo.device.wifi_mac;
    postArr['appid'] = appinfo.device.phone_serial_number;
    postArr['password'] = appinfo.device.baseband;
    postArr['phoneimei'] = appinfo.device.imsi;
    postArr['phoneos'] = appinfo.device.btaddr;
    postArr['decice'] = JSON.stringify(appinfo.device);
    postArr['other'] = other ? other : "成功";
    let data = f.post("http://wenfree.cn/api/Public/idfa/",postArr);
    toastLog("上传成功")
}


function checkIp(){
    url = "http://wenfree.cn/api/Public/idfa/?service=Ip.Checkip&ip="
    r = f.post(url,{})
    log(r)
    if (r){
        r = JSON.parse(r)
        if ( r.data.state == "ok"){
            toastLog(r.data.ip + "可以使用")
            return true
        }
    }
}


// 正式开始编代码
var width = 720;
var height = 1440;
var width = device.width;
var height = device.height;
var phoneMode = device.brand;






log([currentPackage(),currentActivity(),width,height]);
var appinfo = {}
appinfo.name = "锦江";
appinfo.bid = "com.kyscgenuiphone";
appinfo.link = "https://app.dkmobile-genesis.one/";
appinfo.llq = "com.android.chrome";
appinfo.gzbid = "com.deruhai.guangzi";
appinfo.sbdsbid = "com.longene.setcardproperty";
appinfo.sbzs = "com.blue.backup";
info ={};



function main(){
    
    let i = 60
    while ( i > 0 ){
        f.active("com.plateno.botaoota", 5)

        f.ms({"id":"ivAllAgree"}, true, 2)

        f.ms({"textMatches":"今日不再提示"}, true, 2)
        f.ms({"text":"同意并继续使用"}, true, 2)
        f.ms({"text":"跳过"}, true, 3)
        
        f.click(590+5,190+5,1,'x')

        f.ms({"text":"会员"}, true, 2)
        f.ms({"text":"确定"}, true, 2)

        if (f.ms({"text": "登录/注册"}, true, 2)) {
            sleep(5000)
            return true
        }

        i--
        sleep(1000)
    }

}


function all(){

    if (true || hmip() ){

        sleep(1000*2)

        if ( true || sbzs() ){
            if ( true || getsbds() ){
                
                if (active_info()){
                    if (true || main() ){
                        return true
                    }
                }


                f.ms({"textMatches":".*关闭.*"},true,2)
            }
    
            f.ms({"textMatches":".*关闭.*"},true,2)
        }

    }

    f.ms({"textMatches":".*关闭.*"},true,2)
}

console.show();


var read_list = []
read_list.push('123123123')
log(read_list)


// while ( true ){

//     var ipvpn = 0
//     try {

//         all()

//         sleep(1000)


//     } catch (error) {
//         log(error)
//         // hmip_close()
//         sleep(5000)
//     }

// }






